<template>
  <el-dialog
    title="新增员工"
    width="600px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form label-width="120px" :rules="rules" ref="form" :model="form">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker type="date" v-model="form.timeOfEntry" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" readonly @focus="focusEvent" />
        <div>
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker type="date" v-model="form.correctionTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center;">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="isShow=false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/api/constant/employees.js'
import { companyDepartment } from '@/api/department.js'
import { sysUserPost } from '@/api/employees.js'
import { validatePhone } from '@/utils/validate.js'
export default {
  data () {
    return {
      form: {
        username: '', //  string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // 	string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        correctionTime: '' // string	非必须		转正时间
      },
      rules: {
        username: [
          { required: 'true', message: '必填', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' }
        ],
        mobile: [
          { required: 'true', message: '必填', trigger: 'change' },
          { min: 11, max: 11, message: '请输入11位手机号码', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              validatePhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        timeOfEntry: [
          { required: 'true', message: '必填', trigger: 'change' },
        ],
        formOfEmployment: [
          { required: 'true', message: '必填', trigger: 'change' },
        ],
        workNumber: [
          { required: 'true', message: '必填', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' }
        ],
        departmentName: [
          { required: 'true', message: '必填', trigger: 'change' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'change' }
        ],
        correctionTime: [
          { required: 'true', message: '必填', trigger: 'change' },
        ]
      },
      isShow: false,
      employees,
      treeShow: false,
      treeData: []
    }
  },
  methods: {
    async focusEvent () {
      this.treeShow = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts, '')
        console.log('组织架构', res)
      }
    },
    nodeClick (data) {
      this.treeShow = false
      this.form.departmentName = data.name
    },
    changeData (arr, pid = '') {
      return arr.filter(item => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysUserPost(this.form)
          this.isShow = false
          this.$message.success('添加成功')
          this.$emit('getData')
        }
      })
    },
    closeEvent () {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //  string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // 	string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        correctionTime: '' // string	非必须		转正时间
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.add {
}
</style>
